package com.mogujie.tt.ui.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.mogujie.tt.DB.entity.GroupEntity;
import com.mogujie.tt.DB.entity.UserEntity;
import com.mogujie.tt.R;
import com.mogujie.tt.config.NewFriendsConstant;
import com.mogujie.tt.imservice.event.NewFriendsEvent;
import com.mogujie.tt.imservice.manager.IMNewFriendsManager;
import com.mogujie.tt.imservice.support.IMServiceConnector;
import com.mogujie.tt.ui.base.QZBaseActivity;
import com.mogujie.tt.ui.base.TTBaseActivity;
import com.mogujie.tt.ui.widget.IMBaseImageView;
import com.mogujie.tt.utils.IMUIHelper;

import de.greenrobot.event.EventBus;

public class AddFriendsTwoActivity extends QZBaseActivity {
    private RelativeLayout basic_information;
    private IMBaseImageView personalized_avatar;
    private TextView nickname;
    private TextView mobile_phone;
    private boolean signFlag =false;
    private IMServiceConnector imServiceConnector = new IMServiceConnector() {
        @Override
        public void onIMServiceConnected() {

            EventBus.getDefault().registerSticky(AddFriendsTwoActivity.this);//注册事件
        }

        @Override
        public void onServiceDisconnected() {
            if (EventBus.getDefault().isRegistered(AddFriendsTwoActivity.this)) {
                EventBus.getDefault().unregister(AddFriendsTwoActivity.this);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imServiceConnector.connect(this);
        LayoutInflater.from(this).inflate(R.layout.tt_activity_addfriends_two, topContentView);//加载布局
        showTopInformation();

        basic_information =(RelativeLayout)this.findViewById(R.id.basic_information);
        nickname = (TextView)basic_information.findViewById(R.id.nickname);
        mobile_phone =(TextView)basic_information.findViewById(R.id.mobile_phone);
        personalized_avatar =(IMBaseImageView)basic_information.findViewById(R.id.personalized_avatar);

        final RelativeLayout re_search = (RelativeLayout) this.findViewById(R.id.re_search);//搜索布局
        final TextView tv_search = (TextView) re_search.findViewById(R.id.tv_search);//显示输入的
      //  final EditText et_search = (EditText) this.findViewById(R.id.et_search);//输入栏
        topRightSearch.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                if (s.length() > 0) {
                    re_search.setVisibility(View.VISIBLE);
                    tv_search.setText(topRightSearch.getText().toString().trim());
                } else {
                    re_search.setVisibility(View.GONE);
                    tv_search.setText("");
                    basic_information.setVisibility(View.GONE);
                }

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            public void afterTextChanged(Editable s) {

            }
        });
        re_search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String uid = topRightSearch.getText().toString().trim();
                if (uid == null || uid.equals("")) {
                    return;
                }
                searchUser(uid);//查找用户
            }
        });
        basic_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object object =  NewFriendsConstant.userFind;
                if(object instanceof UserEntity){
                    UserEntity userEntity = (UserEntity) object;
                    IMUIHelper.openAddUserProfileActivity(AddFriendsTwoActivity.this, userEntity.getPeerId(),1);
                } else{
                }
            }
        });

    }

    private void searchUser(String uid) {
            signFlag = true;
            IMNewFriendsManager.instance().reqGetFindUser(uid);//查找用户
    }

    public void onClickExit(View view) {

        AddFriendsTwoActivity.this.finish();
    }

    /**
     * 显示顶部信息
     */
    private void showTopInformation(){
        setTopBarDisplay();
        setSearchBar();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        imServiceConnector.disconnect(this);
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
       }

    }

    /**-----------事件--------------------------**/
    public void onEventMainThread(NewFriendsEvent event) {
     //   Log.v("AddFriendsTwoActivity","onEventMainThread -----------------");
        switch (event.getEvent()) {
            case SEARCH_USER:
                searchUser(true);
                break;
            case SEARCH_NO_USER:
                searchUser(false);
                break;
         /*   case FIND_USER_FAIL:
                Toast.makeText(AddFriendsTwoActivity.this,"无网络 查找用户失败！！！",Toast.LENGTH_SHORT).show();
                break;*/
        }
    }

    /**
     * 搜索用户
     * @param flag
     */
    private void searchUser(boolean flag){
        if(flag){
            if(signFlag) {
                basic_information.setVisibility(View.VISIBLE);
                nickname.setText(NewFriendsConstant.userFind.getMainName().toString());
                mobile_phone.setText(NewFriendsConstant.userFind.getRealName().toString());//得到真实姓名
                personalized_avatar.setDefaultImageRes(R.drawable.tt_default_user_portrait_corner);
                personalized_avatar.setCorner(15);
                personalized_avatar.setImageResource(R.drawable.tt_default_user_portrait_corner);
                personalized_avatar.setImageUrl(NewFriendsConstant.userFind.getAvatar());
                signFlag =false;
            }
        }else if(!flag) {
            if(signFlag) {
                Toast.makeText(AddFriendsTwoActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
                signFlag =false;
            }
        }
    }
}